package it.danghongphat.gui.windowms;

import it.danghongphat.business.DaukyBusiness;
import it.danghongphat.gui.MainForm;
import it.danghongphat.object.Dauky;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class DaukyGui extends JDialog{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	
	// Static fields.
	//private static final String NEWLINE = System.getProperty("line.separator");
	
	public JTextField txtmah ;
    JTextField txtmak ;
	JTextField txtsl;
	JTextField txtdg;
	JTextArea txttg;
	public JButton buttonAction;
	JButton btnEdit;
	MainForm mainUI;
	public String oldValue ;

	public DaukyGui(MainForm mainUI){//String title, Icon picture
		super();
		this.mainUI = mainUI; 
		add(streamData());
		this.setAlwaysOnTop(true);
		setSize(360, 270);
		this.setTitle("Form đầu kỳ");
		setResizable(false);
		setLocation((dim.width - getWidth())/2, (dim.height - getHeight())/2);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	public JPanel streamData() 
	{
		JPanel mainPanel = new JPanel();
		
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 10));
		
		
		JPanel helppanel1 = new JPanel(new BorderLayout());
		
		JPanel helppanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
		helppanel2.setPreferredSize(new Dimension(25, 110));
		
		JPanel labelpanel = new JPanel(new GridLayout(5, 1, 0, 0));
		labelpanel.setPreferredSize(new Dimension(70, 130));
		
		JLabel strmah = new JLabel("Mã hàng");
		labelpanel.add(strmah, Component.RIGHT_ALIGNMENT);
		
		JLabel strmak = new JLabel("Mã khách");
		labelpanel.add(strmak, Component.RIGHT_ALIGNMENT);
		
		JLabel strsl = new JLabel("Số lượng");
		labelpanel.add(strsl, Component.RIGHT_ALIGNMENT);
		
		JLabel strdg = new JLabel("Đơn giá");
		labelpanel.add(strdg, Component.RIGHT_ALIGNMENT);
		
		JLabel strtg = new JLabel("Tổng giá");
		labelpanel.add(strtg, Component.RIGHT_ALIGNMENT);
		
		helppanel1.add(labelpanel, BorderLayout.CENTER);
		helppanel1.add(Box.createRigidArea(new Dimension(10, 10)), BorderLayout.WEST);
		helppanel1.add(helppanel2, BorderLayout.SOUTH);
		JPanel comm1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
		comm1.add(helppanel1);
		
		JPanel textpanel = new JPanel( new FlowLayout(FlowLayout.LEFT, 5, 5) );
		textpanel.setPreferredSize(new Dimension(260, 245)); 
		txtmah = new JTextField(10);
		textpanel.add(txtmah);
		
		txtmak = new JTextField(31);
		textpanel.add(txtmak);

		JPanel tenh_mah_panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 5) );
		tenh_mah_panel.setPreferredSize(new Dimension(98, 45));
		txtsl = new JTextField(10);
		tenh_mah_panel.add(txtsl);
		tenh_mah_panel.add(Box.createRigidArea(new Dimension(0, 10)));
		txtdg = new JTextField(10);
		tenh_mah_panel.add(txtdg);
		textpanel.add(tenh_mah_panel);
		tenh_mah_panel.setBorder(BorderFactory.createEmptyBorder(-5, 0, 0, 0));
		
		JPanel dd_panel = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 0) );
		txttg = new JTextArea(5, 31);
		JScrollPane scroll = new  JScrollPane(txttg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		dd_panel.add(scroll);
		textpanel.add(dd_panel);
		
		comm1.add(textpanel);
		
		mainPanel.add(comm1);
		
		JPanel buttonpanel = new JPanel(new GridLayout(1, 4 , 5 , 5) );
  	
		buttonpanel.add(Box.createRigidArea(new Dimension(0, 0)));
		buttonpanel.add(Box.createRigidArea(new Dimension(0, 0)));
		buttonAction = new JButton("Insert");
		buttonAction.addActionListener(new ActionListener() {
			DaukyBusiness daukyBusiness=new DaukyBusiness(null);
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(buttonAction.getText()=="Update"){
					Dauky dk=new Dauky();
					dk.setMah(txtmah.getText().toString().trim());
					dk.setMak(txtmak.getText().toString().trim());
					dk.setSl(Integer.parseInt(txtsl.getText().toString().trim()));
					dk.setDg(Integer.parseInt(txtdg.getText().toString().trim()));
					dk.setTg(Integer.parseInt(txttg.getText().toString().trim()));
					setVisible(false);
					int[] row = mainUI.listdauky.table.getSelectedRows();
					daukyBusiness.update(dk);
					mainUI.listdauky.list.remove(row[0]);
					mainUI.listdauky.list.add(row[0],dk);
					mainUI.listdauky.table.repaint();
				}
				if(buttonAction.getText()=="Insert"){
					Dauky dk=new Dauky();
					dk.setMah(txtmah.getText().toString().trim());
					dk.setMak(txtmak.getText().toString().trim());
					dk.setSl(Integer.parseInt(txtsl.getText().toString().trim()));
					dk.setDg(Integer.parseInt(txtdg.getText().toString().trim()));
					dk.setTg(Integer.parseInt(txttg.getText().toString().trim()));
					setVisible(false);
					daukyBusiness.insert(dk);
					mainUI.listdauky.list.add(dk);
					mainUI.listdauky.table.revalidate();
				}
			}
		});
		oldValue = txtmah.getText().toString().trim();
			
		buttonpanel.add(buttonAction, Component.RIGHT_ALIGNMENT) ;
		
		btnEdit = new JButton("Cancel");
		final DaukyGui themhh;
		themhh = this;
		btnEdit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				themhh.setVisible(false);
			}
		});
		buttonpanel.add(btnEdit, Component.RIGHT_ALIGNMENT) ;
		mainPanel.add(buttonpanel);
        return mainPanel;
	}
	public void setTextMaterial(Dauky dk){
		txtmah.setText(dk.getMah());
		txtmak.setText(dk.getMak());
		txtsl.setText(""+dk.getSl());
		txtdg.setText(""+dk.getDg());
		txttg.setText(""+dk.getTg());
	}
	public void delete(){
		int[] row = mainUI.listdauky.table.getSelectedRows();
		Dauky m=new Dauky();
		m.setMah((String)mainUI.listdauky.table.getValueAt(row[0],0));
		DaukyBusiness daukyBusiness=new DaukyBusiness(null);
		daukyBusiness.delete(m);
		mainUI.listdauky.list.remove(row[0]);
		mainUI.listdauky.table.revalidate();
		
	}
	public void xoa(){
			txtmah.setText("");
			txtmak.setText("");
			txtsl.setText("");
			txtdg.setText("");
			txttg.setText("");
	}
}
